package Test;

import DB.TeacherDB;
import Model.Teacher;
import org.junit.Test;
import java.util.ArrayList;
import static junit.framework.Assert.fail;

public class TeacherDBTest {

    //Ok
    @Test
    public void testInsertTeacher() throws Exception {
        TeacherDB teacherDB = new TeacherDB();

        int id = teacherDB.teacherGetMax();

        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName("Emil Bolet");
        teacher.setUsername("lime");
        teacher.setPassword("emilforprez");
        teacher.setAddress("Sigrid");
        teacher.setEmail("asd@asd.dk");
        teacher.setPhoneNo(88888888);
        teacher.setType("Teacher");
        teacher.setTeacherId(id);
        teacher.setInitials("guan");

        int rc = teacherDB.insertTeacher(teacher);

        if (rc == 1) {
            System.out.println("Inserting teacher/person successful");
        }
        else {
            fail("Failed inserting teacher/person");
        }
    }

    //Ok
    @Test
    public void testFindTeacherByID() throws Exception {
        TeacherDB teacherDB = new TeacherDB();
        int find = 21;

        Teacher teacher = teacherDB.findTeacherByID(find);

        if (teacher != null) {
            System.out.println(teacher.getId());
            System.out.println(teacher.getName());
            System.out.println(teacher.getUsername());
            System.out.println(teacher.getPassword());
            System.out.println(teacher.getAddress());
            System.out.println(teacher.getEmail());
            System.out.println(teacher.getPhoneNo());
            System.out.println(teacher.getType());
            System.out.println(teacher.getTeacherId());
            System.out.println(teacher.getInitials());
        }
        else {
            fail("Failed finding teacher - teacher is null");
        }
    }

    //Ok
    @Test
    public void testFindTeacher() throws Exception {
        TeacherDB teacherDB = new TeacherDB();
        String userName = "kbha@ucn.dk";
        String password = "kbha123";

        Teacher teacher = teacherDB.findTeacher(userName, password);

        if (teacher != null) {
            System.out.println(teacher.getId());
            System.out.println(teacher.getName());
            System.out.println(teacher.getUsername());
            System.out.println(teacher.getPassword());
            System.out.println(teacher.getAddress());
            System.out.println(teacher.getEmail());
            System.out.println(teacher.getPhoneNo());
            System.out.println(teacher.getType());
            System.out.println(teacher.getTeacherId());
            System.out.println(teacher.getInitials());
        }
        else {
            fail("Failed finding teacher - teacher is null");
        }
    }

    //Ok
    @Test
    public void testGetAllTeachers() throws Exception {
        TeacherDB teacherDB = new TeacherDB();
        ArrayList<Teacher> teacherObj = teacherDB.getAllTeachers(false);

        if (teacherObj != null) {
            for (Teacher s : teacherObj) {
                System.out.println(s.getId() + " " + s.getName() + " " + s.getTeacherId());
            }
        }
        else {
            fail("No teachers found");
        }
    }

    //Ok
    @Test
    public void testUpdateTeacher() throws Exception {
        TeacherDB teacherDB = new TeacherDB();

        String updateValue = "Emil Bt";
        int id = 3;

        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(updateValue);
        teacher.setUsername("lime");
        teacher.setPassword("emilforprez");
        teacher.setAddress("Sigrid");
        teacher.setEmail("asd@asd.dk");
        teacher.setPhoneNo(88888888);
        teacher.setType("Teacher");
        teacher.setTeacherId(id);
        teacher.setInitials("guan");

        teacherDB.updateTeacher(teacher);

        String teacherName = teacherDB.findTeacherByID(id).getName();

        if (teacherName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }
    }

    //Ok
    @Test
    public void testDeleteTeacher() throws Exception {
        TeacherDB teacherDB = new TeacherDB();
        Teacher teacher = new Teacher(1000, "Delete", "Delete", "Delete", "Delete", "Delete", 123123123, "Delete", 1000, "guan");
        teacherDB.insertTeacher(teacher);

        if (teacherDB.findTeacherByID(1000) != null) {

            teacherDB.deleteTeacher(1000);

            if (teacherDB.findTeacherByID(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
